package siwi.map.android;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kankan.wheel.widget.NumericWheelAdapter;
import kankan.wheel.widget.WheelView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.wmw.Animation.ViewAction;
import com.wmw.DataPool.OrdersFormItem;
import com.wmw.DataPool.OrdersForm;
import com.wmw.Utility.Common;
import com.wmw.Utility.Dish;
import com.wmw.Utility.MSG;
import com.wmw.Utility.RegularExpression;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import siwi.map.android.R;

public class OrderFormDetail extends SherlockActivity {
	List<RelativeLayout> _phoneNumberList = new ArrayList<RelativeLayout>();
	List<RelativeLayout> _timeList = new ArrayList<RelativeLayout>();

	TextView totalNum;
	EditText remark, address, phoneNumber, totalPrice;
	
	// private ActionBar mActionBar;
	private final Handler mHandler = new Handler();

	private TextView _positiveComment, _moderateComment, _negativeComment;
	private InputBox _price, _name;

	// Sale states.
	private ImageView _forSaleCheck, _toBeSoldOutCheck, _soldOutCheck;
	private Button _forSale, _toBeSoldOut, _soldOut;

	private WheelView _leftNum;
	private Button _leftView, _partView;

	Dish.DishSalesState _curSaleState;

	private Dish _dish;

	private static final int INTERNET_FAILURE = -1;
	private static final int LOAD_SUCCESS = 1;
	private static final int LOAD_MORE_SUCCESS = 3;
	private static final int NO_MORE_INFO = 4;
	private static final int LOAD_NEW_INFO = 5;

	private ProgressBar moreProgressBar;

	private List<String> dataList;
	private ArrayAdapter<String> adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		initActionBar();

		setContentView(R.layout.order_form_detail);

		initViews();
	}
	
	private void initActionBar(){
		// Custom action bar.
		ActionBar actionBar = getSupportActionBar();
		ActionBar.LayoutParams params = new ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
				Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate(
				R.layout.order_form_detail_action_bar, null);
		actionBar.setCustomView(view, params);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);

		TextView tv = (TextView)view.findViewById(R.id.OFDAB_Title);
		tv.setText("订单详细信息");
		
		view.findViewById(R.id.OFDAB_Call).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				// Call.
                String pn = phoneNumber.getText().toString();
                Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+pn));  
                startActivity(intent);  
			}
			
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		OrdersForm of = (OrdersForm) getIntent().getSerializableExtra(MSG.OrderForm);
		OrdersFormItem []items = of.get_items();
		for (int i = 0; i < items.length; ++i){
			OrdersFormItem item = items[i];
			addOneOrder("[" + (i + 1) + "]" + item.get_name(), item.get_totalNum(), item.get_totalNum()*item.get_unitPrice());
		}
		totalNum.setText("合计:" + of.get_totalNum() + "份");
		totalPrice.setText("￥" + of.get_totalPrice());
		remark.setText(of.get_remark());
		address.setText(of.get_address());
		phoneNumber.setText(of.get_phoneNumber());
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			setResult(Activity.RESULT_CANCELED);
			goBack();
		}

		return super.onKeyDown(keyCode, event);
	}

	private void goBack() {
//		Intent intent = new Intent();
//		intent.setClass(OrderFormDetail.this, Home.class);
//		intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//		startActivity(intent);
		OrderFormDetail.this.finish();
		overridePendingTransition(R.anim.left_in, R.anim.right_out);
	}


	private void initViews() {
		// Total numbers
		totalNum = (TextView) findViewById(R.id.OFD_TotalNum);
		totalPrice = (EditText) findViewById(R.id.OFD_TotalPrice);

		remark = (EditText) findViewById(R.id.OFD_Remark);
		address = (EditText) findViewById(R.id.OFD_Address);
		phoneNumber = (EditText) findViewById(R.id.OFD_PhoneNumber);
		findViewById(R.id.OFD_Receive).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				setResult(Activity.RESULT_OK);
				goBack();
			}
			
		});
		
	}

	private void addOneOrder(String name, int num, float price) {
		LayoutInflater inflater = LayoutInflater.from(OrderFormDetail.this);
		LinearLayout layout = (LinearLayout) inflater.inflate(
				R.layout.order_form_detail_item, null).findViewById(
				R.id.OFDI_Layout);

		TextView tv = (TextView) layout.findViewById(R.id.OFDI_Name);
		tv.setText(name + " " + num + "份");

		EditText et = (EditText) layout.findViewById(R.id.OFDI_Price);
		et.setText("￥" + price);

		LinearLayout lin = (LinearLayout) findViewById(R.id.OFD_OrdersFormLayout);
		lin.addView(layout);

	}
}